package com.phamloc.ga01.objects;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.phamloc.ga01.manager.ResourcesManager;

public abstract class Gach extends AnimatedSprite{
	
	private Body body;
	private boolean canThrow = false;
	private int footContacts = 0;
	public Gach(float pX, float pY, VertexBufferObjectManager vbo, Camera camera, PhysicsWorld physicsWorld)
    {
        super(pX, pY, ResourcesManager.getInstance().gach_region, vbo);
        createPhysics(camera, physicsWorld);
    }
	public abstract void Disappear();
	private void createPhysics(final Camera camera, PhysicsWorld physicsWorld) {
		body = PhysicsFactory.createBoxBody(physicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0, 0, 0));
		body.setUserData("gach");
		body.setFixedRotation(true);
		physicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, false){
			@Override
	        public void onUpdate(float pSecondsElapsed)
	        {
				super.onUpdate(pSecondsElapsed);
				camera.onUpdate(0.1f);
				if(getY() <= 0 || getX() >= 480){
					Disappear();
				}
				if (canThrow)
	            {    
	                body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, body.getLinearVelocity().y)); 
	            }
	        }
		});
	}
	public void setThrow() {
		canThrow = true;
		long Animate[] = new long[]{500,500,500,500,500,500,500,500,500,500,500};
		animate(Animate, 0, 10, true);
		away();
	}
	public void away()
	{
		if(footContacts < 0){
			return;
		}
	    body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, 10)); 
	}
	public void increaseFootContacts()
	{
		footContacts++;
	}
	
	public void decreaseFootContacts()
	{
		footContacts--;
	}
}
